home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap05 / Bezier / Bezier.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  4.0 KB  |  135 lines

  1. /*---------------------------------------
  2.    BEZIER.C -- Bezier Splines Demo
  3.                (c) Charles Petzold, 1998
  4.   ---------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
  9.  
  10. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  11.                     PSTR szCmdLine, int iCmdShow)
  12. {
  13.      static TCHAR szAppName[] = TEXT ("Bezier") ;
  14.      HWND         hwnd ;
  15.      MSG          msg ;
  16.      WNDCLASS     wndclass ;
  17.      
  18.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  19.      wndclass.lpfnWndProc   = WndProc ;
  20.      wndclass.cbClsExtra    = 0 ;
  21.      wndclass.cbWndExtra    = 0 ;
  22.      wndclass.hInstance     = hInstance ;
  23.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  24.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  25.      wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
  26.      wndclass.lpszMenuName  = NULL ;
  27.      wndclass.lpszClassName = szAppName ;
  28.      
  29.      if (!RegisterClass (&wndclass))
  30.      {
  31.           MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
  32.                       szAppName, MB_ICONERROR) ;
  33.           return 0 ;
  34.      }
  35.      
  36.      hwnd = CreateWindow (szAppName, TEXT ("Bezier Splines"),
  37.                           WS_OVERLAPPEDWINDOW,
  38.                           CW_USEDEFAULT, CW_USEDEFAULT,
  39.                           CW_USEDEFAULT, CW_USEDEFAULT,
  40.                           NULL, NULL, hInstance, NULL) ;
  41.      
  42.      ShowWindow (hwnd, iCmdShow) ;
  43.      UpdateWindow (hwnd) ;
  44.      
  45.      while (GetMessage (&msg, NULL, 0, 0))
  46.      {
  47.           TranslateMessage (&msg) ;
  48.           DispatchMessage (&msg) ;
  49.      }
  50.      return msg.wParam ;
  51. }
  52.  
  53. void DrawBezier (HDC hdc, POINT apt[])
  54. {
  55.      PolyBezier (hdc, apt, 4) ;
  56.      
  57.      MoveToEx (hdc, apt[0].x, apt[0].y, NULL) ;
  58.      LineTo   (hdc, apt[1].x, apt[1].y) ;
  59.      
  60.      MoveToEx (hdc, apt[2].x, apt[2].y, NULL) ;
  61.      LineTo   (hdc, apt[3].x, apt[3].y) ;
  62. }
  63.  
  64. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  65. {
  66.      static POINT apt[4] ;
  67.      HDC          hdc ;
  68.      int          cxClient, cyClient ;
  69.      PAINTSTRUCT  ps ;
  70.      
  71.      switch (message)
  72.      {
  73.      case WM_SIZE:
  74.           cxClient = LOWORD (lParam) ;
  75.           cyClient = HIWORD (lParam) ;
  76.           
  77.           apt[0].x = cxClient / 4 ;
  78.           apt[0].y = cyClient / 2 ;
  79.           
  80.           apt[1].x = cxClient / 2 ;
  81.           apt[1].y = cyClient / 4 ;
  82.           
  83.           apt[2].x =     cxClient / 2 ;
  84.           apt[2].y = 3 * cyClient / 4 ;
  85.           
  86.           apt[3].x = 3 * cxClient / 4 ;
  87.           apt[3].y =     cyClient / 2 ;
  88.           
  89.           return 0 ;
  90.  
  91.      case WM_LBUTTONDOWN:
  92.      case WM_RBUTTONDOWN:
  93.      case WM_MOUSEMOVE:
  94.           if (wParam & MK_LBUTTON || wParam & MK_RBUTTON)
  95.           {
  96.                hdc = GetDC (hwnd) ;
  97.                
  98.                SelectObject (hdc, GetStockObject (WHITE_PEN)) ;
  99.                DrawBezier (hdc, apt) ;
  100.                
  101.                if (wParam & MK_LBUTTON)
  102.                {
  103.                     apt[1].x = LOWORD (lParam) ;
  104.                     apt[1].y = HIWORD (lParam) ;
  105.                }
  106.                
  107.                if (wParam & MK_RBUTTON)
  108.                {
  109.                     apt[2].x = LOWORD (lParam) ;
  110.                     apt[2].y = HIWORD (lParam) ;
  111.                }
  112.                
  113.                SelectObject (hdc, GetStockObject (BLACK_PEN)) ;
  114.                DrawBezier (hdc, apt) ;
  115.                ReleaseDC (hwnd, hdc) ;
  116.           }
  117.           return 0 ;
  118.           
  119.      case WM_PAINT:
  120.           InvalidateRect (hwnd, NULL, TRUE) ;
  121.           
  122.           hdc = BeginPaint (hwnd, &ps) ;
  123.           
  124.           DrawBezier (hdc, apt) ;
  125.           
  126.           EndPaint (hwnd, &ps) ;
  127.           return 0 ;
  128.           
  129.      case WM_DESTROY:
  130.           PostQuitMessage (0) ;
  131.           return 0 ;
  132.      }
  133.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  134. }
  135.